package models

import (
	"github.com/astaxie/beego/orm"
	"t11Lib/t11Errors"
	"亚太-iot-服务器/conf"
)

func NewEmqOrmer() (orm.Ormer, t11Errors.T11Error) {
	db, err := orm.GetDB("emqAuth")
	if err != nil {
		return nil, t11Errors.Wrap(err)
	}
	o, err := orm.NewOrmWithDB("mysql", "emqAuth", db)
	if err != nil {
		return nil, t11Errors.Wrap(err)
	}
	return o, nil
}

func init() {
	_ = orm.RegisterDriver("mysql", orm.DRMySQL)
	_ = orm.RegisterDataBase("default", "mysql", conf.AppConfig.DbUrl)
	_ = orm.RegisterDataBase("emqAuth", "mysql", conf.AppConfig.EmqAuthDbUrl)
	orm.RegisterModel(&MqttUser{}, &MqttAcl{}, &Gateway{}, &WxUser{}, &Device{}, &DeviceKv{}, &DeviceLog{}, &UserDevice{}, &UserDeviceKv{}, &UserGatewayKv{},&Guarantee{})
	_ = orm.RunSyncdb("default", false, true)
	_ = orm.RunSyncdb("emqAuth", false, true)
}
